package com.amanda.suaFa.sometests;

import java.util.Arrays;

/**
 * @author amanda
 * @Description 二分查找
 */
public class BinarySearch {
    public static int rank(int key, int[] a) {
        // 数组必须是有序的
        int lo = 0;
        int hi =  a.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (key < a[mid])
                hi = mid - 1;
            else if (key > a[mid])
                lo = mid + 1;
            else
                return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] writeList = {10,16,18,23,33,48,54,77,84};
        //Arrays.sort(writeList);
        int key = 23;
        if(rank(key,writeList) < 0) {
            System.out.println(key);
        }
    }

}
